package com.wing.ext.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.ext.domain.Messageboard;
import com.wing.ext.service.MessageboardManager;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/ext/messageboard")
public class MessageboardController {

	@Autowired
	private MessageboardManager messageboardManager;
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/ext/messageboard/messageboardIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/ext/messageboard/"+url;
	}
		
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(String sval,int currentPage, String pageSize,HttpServletRequest request) {
		int _iPageSize = 0;
		String siteId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_ID);
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  messageboardManager.findPageList(currentPage, _iPageSize,siteId,sval);
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id, Model model) {
		Messageboard messageboard = null;
		if(!id.equals("0")){
			messageboard = messageboardManager.findById(id);
		}else{
			messageboard = new Messageboard();
		}
		model.addAttribute("messageboard", messageboard);
		return "/ext/messageboard/messageboardForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Messageboard messageboard,HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_ID);
		messageboard.setSite_id(siteId);
		messageboardManager.saveMessageboard(messageboard);
		return "success";
	}
	
	@RequestMapping(value = "send", method = RequestMethod.POST)
	public  @ResponseBody  String send(Messageboard messageboard,String a, HttpServletRequest request, HttpServletResponse response) {
		
		String validateC = (String) request.getSession().getAttribute("validateCode");
		String veryCode = request.getParameter("veryCode");
		if("t".equals(a)){
			veryCode="A";
			validateC="A";
		}
		if(veryCode==null||"".equals(veryCode)){
			return "codenull";
		}else{
			if(!validateC.equals(veryCode.toUpperCase())){ 
				return "failure";          
			}              
		} 
		String siteId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_ID);
		messageboard.setSite_id(siteId);
		messageboardManager.saveMessageboard(messageboard);
		return "success";
	}
	
	@RequestMapping(value = "delmessageboards", method = RequestMethod.POST)
	public @ResponseBody String delmessageboards(String[] chk) throws IOException {
		messageboardManager.deleteMessageboards(chk);
		return ""+chk.length;
	}
}

